<template>
  <div ref="background" id="animation-container"></div>
  <div class="background"></div>
  <div class="container" :class="{ flip: register }">
    <div class="login-section" :style="{ opacity: register ? 0 : 1 }">
      <el-image
          style="height: 80px; margin-bottom: 30px;"
          src="https://picsum.photos/id/1/200/80"
          fit="contain"
          alt="品牌Logo"
      />
      <el-form label-width="auto">
        <el-tabs @tab-change="tabChange" stretch tab-position="top" class="demo-tabs">
          <el-tab-pane label="账号密码登录">
            <el-form-item label="登录编码">
              <el-input
                  prefix-icon="User"
                  v-model="adminsForm.adminCode"
                  placeholder="请输入登录编码"
                  style="width: 280px;"
                  clearable
              />
            </el-form-item>
            <el-form-item label="登录密码">
              <el-input
                  prefix-icon="Lock"
                  v-model="adminsForm.password"
                  show-password
                  placeholder="请输入登录密码"
                  style="width: 280px;"
                  clearable
              />
            </el-form-item>
          </el-tab-pane>
        </el-tabs>
        <el-form-item label="" class="el-button-group">
          <el-button @click="login" type="primary" :loading="loading">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="carousel-section">
      <el-carousel @change="updateBg" height="100%" arrow="always" motion-blur>
        <el-carousel-item v-for="(image, index) in images" :key="index">
          <img :src="image" alt="背景图" class="carousel-image"/>
        </el-carousel-item>
      </el-carousel>
    </div>
    <div class="register" @click="toggleCard">
      <span v-if="register" class="el-icon__label" style="color: white;transform: rotateY(180deg)">返回登录</span>
      <span v-else class="el-icon__label" style="color: white">欢迎管理员登录</span>
      <el-icon v-if="register" color="white">
        <ArrowRight/>
      </el-icon>
    </div>
    <div class="card-edge top"></div>
    <div class="card-edge bottom"></div>
    <div class="card-edge left"></div>
    <div class="card-edge right"></div>
  </div>
</template>

<script setup>
import { inject, reactive, ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { ArrowRight, User, Lock } from "@element-plus/icons-vue";
import { setSessionStorage } from "../common.js";
import { useRouter } from 'vue-router';

const myElMessage = inject('myElMessage');
const axios = inject('axios');
const router = useRouter();
const loading = ref(false);

// 背景轮播图配置（使用稳定的外网图片链接）
const images = ref([
  'https://picsum.photos/id/1035/800/1200',
  'https://picsum.photos/id/1036/800/1200',
  'https://picsum.photos/id/1037/800/1200',
]);

const background = ref(null);
const register = ref(false);

// 初始化表单数据
const adminsForm = reactive({
  adminCode: "",
  password: "",
});

// 切换登录/注册卡片
const toggleCard = () => {
  register.value = !register.value;
};

// 更新背景图
const updateBg = (current) => {
  if (background.value) {
    background.value.style.backgroundImage = `url(${images.value[current]})`;
  }
};

// 登录逻辑
const login = async () => {
  try {
    loading.value = true;

    // 表单验证
    if (!adminsForm.adminCode) {
      myElMessage('登录编码不能为空！', 'warning');
      return;
    }
    if (!adminsForm.password) {
      myElMessage('登录密码不能为空！', 'warning');
      return;
    }

    // 调用后端接口
    const response = await axios.post("admins/getAdminsByCodeByPass", adminsForm);

    if (response.data) {
      setSessionStorage('admins', response.data);
      myElMessage('登录成功', 'success');
      setTimeout(() => {
        router.push('/index');
      }, 1000);
    } else {
      myElMessage('登录编码或密码不正确', 'error');
    }
  } catch (error) {
    console.error('登录请求失败:', error);
    myElMessage('登录请求失败，请稍后再试', 'error');
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  updateBg(0);
});
</script>

<style scoped>
#animation-container {
  position: absolute;
  width: 100%;
  height: 100%;
  transition: all 0.6s;
  pointer-events: none;
}

.background {
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  position: absolute;
  background-color: rgba(0, 0, 0, 0.3);
  z-index: -1;
}

.register {
  box-shadow: 0 0 8px 0 rgba(0, 0, 0, 0.3);
  cursor: pointer;
  opacity: 0.9;
  width: 140px;
  height: 50px;
  position: absolute;
  top: 20px;
  left: 20px;
  background-color: #409EFF;
  border-bottom-right-radius: 25px;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.3s;
  z-index: 10;
}

.register:hover {
  opacity: 1;
  transform: translateY(-3px);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);
}

.el-button-group:deep(.el-form-item__content) {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}

.el-button-group:deep(button) {
  padding: 0 30px;
  height: 40px;
  border-radius: 20px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.2);
  border: none;
  font-size: 16px;
  transition: all 0.3s;
}

.el-button-group:deep(button):hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);
}

.container {
  background-color: white;
  display: flex;
  position: absolute;
  justify-content: center;
  align-items: center;
  width: 900px;
  height: 600px;
  border-radius: 30px;
  overflow: hidden;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  transform-style: preserve-3d;
  transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 5;
}

.container.flip {
  transform: rotateY(180deg) translate(-50%, -50%);
}

.login-section {
  transition: opacity 0.6s;
  width: 50%;
  height: 100%;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  padding: 40px;
  box-sizing: border-box;
  backface-visibility: hidden;
}

.carousel-section {
  width: 50%;
  height: 100%;
  position: relative;
}

.el-carousel {
  height: 100%; /* 关键：确保轮播图容器有明确高度 */
}

.el-carousel__container {
  height: 100%;
  width: 100%;
}

.el-carousel__item {
  height: 100%;
  width: 100%;
}

.el-carousel__item img {
  width: 100%;
  height: 100%;
  object-fit: cover; /* 保持图片比例并覆盖容器 */
}

.container:deep(ul.el-carousel__indicators) {
  bottom: 20px;
}

.container:deep(ul.el-carousel__indicators li.el-carousel__button) {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.5);
  transition: all 0.3s;
}

.container:deep(ul.el-carousel__indicators li.el-carousel__button.is-active) {
  background-color: white;
  transform: scale(1.2);
}

.title {
  margin-bottom: 30px;
  font-size: 24px;
  font-weight: bold;
  color: #333;
  text-align: center;
}

.el-input {
  width: 100%;
  transition: all 0.3s;
}

.el-input:focus-within {
  transform: translateY(-2px);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
}

.el-input__inner {
  height: 40px;
  border-radius: 20px;
  border: 1px solid #dcdfe6;
  transition: all 0.3s;
}

.el-input__inner:focus {
  border-color: #409EFF;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.card-edge {
  position: absolute;
  width: calc(100% + 2px);
  height: calc(100% + 2px);
  background-color: #dcdfe6;
  transform-origin: center;
  z-index: -1;
}

.card-edge.top {
  top: -1px;
  transform: rotateX(90deg) translateZ(-1px);
}

.card-edge.bottom {
  bottom: -1px;
  transform: rotateX(-90deg) translateZ(-1px);
}

.card-edge.left {
  left: -1px;
  transform: rotateY(-90deg) translateZ(-1px);
}

.card-edge.right {
  right: -1px;
  transform: rotateY(90deg) translateZ(-1px);
}

@media (max-width: 900px) {
  .container {
    width: 90%;
    height: 90%;
  }

  .login-section, .carousel-section {
    width: 100%;
  }

  .carousel-section {
    display: none;
  }

  .register {
    top: 15px;
    left: 15px;
    width: 120px;
    height: 40px;
  }
}
</style>

<style>
#animation-container {
  position: absolute;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.shape {
  position: absolute;
  opacity: 0.5;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
  mix-blend-mode: screen;
  animation: rotate 40s linear infinite;
}

.shape.circle {
  border-radius: 50%;
}

.shape.square {
  border-radius: 10%;
}

@keyframes rotate {
  from {
    transform: rotate(0deg) translate(-50%);
  }
  to {
    transform: rotate(360deg) translate(-50%);
  }
}

.carousel-image {
  transition: transform 0.5s ease;
}

.el-carousel__item:hover .carousel-image {
  transform: scale(1.03);
}
</style>